const express = require('express')
const auth = require('../middleware/auth')
const router = express.Router()

// 获取设备列表
router.get('/', auth, (req, res) => {
  try {
    const devices = [
      {
        id: '1',
        name: '温度传感器-01',
        type: 'temperature',
        status: 'online',
        location: 'A区1楼',
        value: 23.5,
        unit: '°C',
        lastUpdate: new Date().toISOString()
      },
      {
        id: '2',
        name: '湿度传感器-01',
        type: 'humidity',
        status: 'online',
        location: 'A区1楼',
        value: 65,
        unit: '%',
        lastUpdate: new Date().toISOString()
      }
    ]
    
    res.json({
      success: true,
      data: devices,
      total: devices.length
    })
  } catch (error) {
    console.error('Get devices error:', error)
    res.status(500).json({
      success: false,
      error: '获取设备列表失败'
    })
  }
})

// 获取单个设备信息
router.get('/:id', auth, (req, res) => {
  try {
    const { id } = req.params
    const device = {
      id,
      name: `设备-${id}`,
      type: 'sensor',
      status: 'online',
      location: 'A区1楼',
      value: Math.random() * 100,
      unit: '单位',
      lastUpdate: new Date().toISOString()
    }
    
    res.json({
      success: true,
      data: device
    })
  } catch (error) {
    console.error('Get device error:', error)
    res.status(500).json({
      success: false,
      error: '获取设备信息失败'
    })
  }
})

module.exports = router